{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 58097 entries, 0 to 58096\n",
      "Data columns (total 5 columns):\n",
      "r_key         58097 non-null object\n",
      "out_id        58097 non-null object\n",
      "start_time    58097 non-null object\n",
      "start_lat     58097 non-null float64\n",
      "start_lon     58097 non-null float64\n",
      "dtypes: float64(2), object(3)\n",
      "memory usage: 2.2+ MB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>r_key</th>\n",
       "      <th>out_id</th>\n",
       "      <th>start_time</th>\n",
       "      <th>start_lat</th>\n",
       "      <th>start_lon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...</td>\n",
       "      <td>358962079107966</td>\n",
       "      <td>2018-09-01 15:54:12</td>\n",
       "      <td>43.943356</td>\n",
       "      <td>125.377718</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...</td>\n",
       "      <td>358962079111695</td>\n",
       "      <td>2018-09-01 13:16:11</td>\n",
       "      <td>43.886501</td>\n",
       "      <td>125.272971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...</td>\n",
       "      <td>358962079120563</td>\n",
       "      <td>2018-09-01 18:08:36</td>\n",
       "      <td>43.867917</td>\n",
       "      <td>125.307853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...</td>\n",
       "      <td>4A23256745CBA3B0</td>\n",
       "      <td>2018-09-01 17:39:09</td>\n",
       "      <td>32.843961</td>\n",
       "      <td>115.864957</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...</td>\n",
       "      <td>851181601098201</td>\n",
       "      <td>2018-09-01 09:10:15</td>\n",
       "      <td>22.711616</td>\n",
       "      <td>113.318815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               r_key            out_id  \\\n",
       "0  f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...   358962079107966   \n",
       "1  a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...   358962079111695   \n",
       "2  7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...   358962079120563   \n",
       "3  7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...  4A23256745CBA3B0   \n",
       "4  d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...   851181601098201   \n",
       "\n",
       "            start_time  start_lat   start_lon  \n",
       "0  2018-09-01 15:54:12  43.943356  125.377718  \n",
       "1  2018-09-01 13:16:11  43.886501  125.272971  \n",
       "2  2018-09-01 18:08:36  43.867917  125.307853  \n",
       "3  2018-09-01 17:39:09  32.843961  115.864957  \n",
       "4  2018-09-01 09:10:15  22.711616  113.318815  "
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "testdf = pd.read_csv('/home/data/test_new.csv')\n",
    "testdf.info()\n",
    "testdf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import math\n",
    "import sys\n",
    "import time\n",
    "from datetime import date\n",
    "\n",
    "import numpy as np\n",
    "from sklearn.externals import joblib\n",
    "\n",
    "import geohash\n",
    "import argparse\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_file = 'conbinRF03_1106.pkl'\n",
    "regressor = joblib.load(model_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def date_extractor(date_str,b,minutes_per_bin):\n",
    "    # Takes a datetime object as a parameter\n",
    "    # and extracts and returns a tuple of the form: (as per the data specification)\n",
    "    # (time_cat, time_num, time_cos, time_sin, day_cat, day_num, day_cos, day_sin, weekend)\n",
    "    # Split date string into list of date, time\n",
    "    \n",
    "    d = date_str.split()\n",
    "    \n",
    "    #safety check\n",
    "    if len(d) != 2:\n",
    "        return tuple([None,])\n",
    "    \n",
    "    # TIME (eg. for 16:56:20 and 15 mins per bin)\n",
    "    #list of hour,min,sec (e.g. [16,56,20])\n",
    "    time_list = [int(t) for t in d[1].split(':')]\n",
    "    \n",
    "    #safety check\n",
    "    if len(time_list) != 3:\n",
    "        return tuple([None,])\n",
    "    \n",
    "    # calculate number of minute into the day (eg. 1016)\n",
    "    num_minutes = time_list[0] * 60 + time_list[1]\n",
    "    \n",
    "    # Time of the start of the bin\n",
    "    time_bin = num_minutes / minutes_per_bin     # eg. 1005\n",
    "    hour_bin = num_minutes / 60                  # eg. 16\n",
    "    min_bin = (time_bin * minutes_per_bin) % 60  # eg. 45\n",
    "    \n",
    "    #get time_cat\n",
    "    hour_str = str(hour_bin) if hour_bin / 10 > 0 else \"0\" + str(hour_bin)  # eg. \"16\"\n",
    "    min_str = str(min_bin) if min_bin / 10 > 0 else \"0\" + str(min_bin)      # eg. \"45\"\n",
    "    time_cat = hour_str + \":\" + min_str                                     # eg. \"16:45\"\n",
    "    \n",
    "    # Get a floating point representation of the center of the time bin\n",
    "    time_num = (hour_bin*60 + min_bin + minutes_per_bin / 2.0)/(60*24)      # eg. 0.7065972222222222\n",
    "    \n",
    "    time_cos = math.cos(time_num * 2 * math.pi)\n",
    "    time_sin = math.sin(time_num * 2 * math.pi)\n",
    "    \n",
    "    # DATE\n",
    "    # Parse year, month, day\n",
    "    date_list = d[0].split('-')\n",
    "    d_obj = date(int(date_list[0]),int(date_list[1]),int(date_list[2]))\n",
    "    day_to_str = {0: \"Monday\",\n",
    "                  1: \"Tuesday\",\n",
    "                  2: \"Wednesday\",\n",
    "                  3: \"Thursday\",\n",
    "                  4: \"Friday\",\n",
    "                  5: \"Saturday\",\n",
    "                  6: \"Sunday\"}\n",
    "    day_of_week = d_obj.weekday()\n",
    "    day_cat = day_to_str[day_of_week]\n",
    "    day_num = (day_of_week + time_num)/7.0\n",
    "    day_cos = math.cos(day_num * 2 * math.pi)\n",
    "    day_sin = math.sin(day_num * 2 * math.pi)\n",
    "    \n",
    "    year = d_obj.year\n",
    "    month = d_obj.month\n",
    "    day = d_obj.day\n",
    "    \n",
    "    weekend = 0\n",
    "    #check if it is the weekend\n",
    "    if day_of_week in [5,6]:\n",
    "        weekend = 1\n",
    "       \n",
    "    return (year, month, day, time_cat, time_num, time_cos, time_sin, day_cat, day_num, day_cos, day_sin, weekend)\n",
    "\n",
    "def bucketed_location(lat, lon):\n",
    "    location = geohash.encode(float(lat), float(lon), g)\n",
    "    return geohash.decode(location)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>r_key</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               r_key\n",
       "0  f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...\n",
       "1  a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...\n",
       "2  7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...\n",
       "3  7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...\n",
       "4  d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378..."
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultDf = pd.DataFrame()\n",
    "resultDf.loc[:,\"r_key\"] = testdf.loc[:,\"r_key\"]\n",
    "resultDf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>r_key</th>\n",
       "      <th>end_lat</th>\n",
       "      <th>end_lon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               r_key  end_lat  end_lon\n",
       "0  f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...        0        0\n",
       "1  a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...        0        0\n",
       "2  7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...        0        0\n",
       "3  7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...        0        0\n",
       "4  d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...        0        0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(testdf)\n",
    "resultDf['end_lat'] = 0\n",
    "resultDf['end_lon'] = 0\n",
    "resultDf.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>r_key</th>\n",
       "      <th>end_lat</th>\n",
       "      <th>end_lon</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...</td>\n",
       "      <td>43.914452</td>\n",
       "      <td>119.038322</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...</td>\n",
       "      <td>43.886620</td>\n",
       "      <td>120.825457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...</td>\n",
       "      <td>43.979001</td>\n",
       "      <td>105.522776</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...</td>\n",
       "      <td>32.965117</td>\n",
       "      <td>109.141026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...</td>\n",
       "      <td>25.478207</td>\n",
       "      <td>112.999175</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               r_key    end_lat     end_lon\n",
       "0  f6fa6b2a1fa250b3_SDK-XJ_eed80f24f496fc9a59f49e...  43.914452  119.038322\n",
       "1  a584728d1eb0fb5b_SDK-XJ_d60de6f0b8121b07383e80...  43.886620  120.825457\n",
       "2  7308d46abc5ec4d0_SDK-XJ_6dd3f0f118e9813c51ed22...  43.979001  105.522776\n",
       "3  7038defae966f837_SDK-XJ_c596c8fd60e6e6cad14518...  32.965117  109.141026\n",
       "4  d4324c1d7d32d377_SDK-XJ_181a752a238a3f720eb378...  25.478207  112.999175"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(len(testdf)):\n",
    "    startLat = float(testdf.iloc[i,3])\n",
    "    startLon = float(testdf.iloc[i,4])\n",
    "    x_start = math.cos(startLat) * math.cos(startLon)\n",
    "    y_start = math.cos(startLat) * math.sin(startLon) \n",
    "    z_start = math.sin(startLat) \n",
    "    zippedFeatures = date_extractor(testdf.iloc[i,2], 12, 15)\n",
    "    parameters = np.array((zippedFeatures[8],zippedFeatures[1]/12, x_start, y_start,z_start)).reshape(1, -1)\n",
    "    prediction = regressor.predict(parameters)\n",
    "    resultDf.iloc[i,1] = prediction[0][0]\n",
    "    resultDf.iloc[i,2] = prediction[0][1]\n",
    "resultDf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultDf.to_csv('conbinRF03.csv',index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultDf.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "resultDf.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "testdf.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
